#include "include/swei_reboot.h"
#include "include/swei_cmd.h"
#include "global.h"
#include "include/port/swei_log.h"
#include "service/include/swei_downlink.h"
#include "misc/misc_api.h"
#include "comm/co_heartbeat.h"

/**
 * @brief 重启操作回调
 * @param  cmd_id           My Param doc
 * @param  cmd_opcode       My Param doc
 * @param  p_data           My Param doc
 * @param  data_len         My Param doc
 * @return DLCMD_RET_TYPE_ENUM
 */
static DLCMD_RET_TYPE_ENUM reboot_event_callback(DOWN_LINK_CMD_ENUM cmd_id, uint16_t cmd_opcode, const unsigned char *p_data, uint32_t data_len)
{
    exbatt_stat_t stat = {0};

    exbat_stat_get((uint8_t*)&stat, sizeof(exbatt_stat_t));
    
    /* 1.判断当前是否外电在位, 如果外电在位不允许执行重启(也不需要应答) */
    if (stat.supply) {
        log_i("main battery exist, cannot exec reboot\r\n");
        return DLCMD_RET_CONTINUE;
    }

    /* 2.重启仪表 */
    co_reset();

    /* 3.重启IOT */
    tbox_system_reboot(1000);
    return DLCMD_RET_FINISH;
}

/**
 * @brief 重启功能初始化
 */
void swei_reboot_init(void)
{
    swei_downlink_cmd_register(DOWN_LINK_CMD_TBOX_REBOOT, reboot_event_callback);
}
